<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>NumCpp: GaussNewtonNlls.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">NumCpp
   &#160;<span id="projectnumber">1.0</span>
   </div>
   <div id="projectbrief">A C++ implementation of the Python Numpy library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_gauss_newton_nlls_8cpp-example.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">GaussNewtonNlls.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<h1><a class="anchor" id="Description"></a>
Description</h1>
<p>A C++ Implementation of the Python Numpy Library</p>
<dl class="section author"><dt>Author</dt><dd>David Pilger <a href="#" onclick="location.href='mai'+'lto:'+'dpi'+'lg'+'er2'+'6@'+'gma'+'il'+'.co'+'m'; return false;">dpilg<span style="display: none;">.nosp@m.</span>er26<span style="display: none;">.nosp@m.</span>@gmai<span style="display: none;">.nosp@m.</span>l.co<span style="display: none;">.nosp@m.</span>m</a> <a href="https://github.com/dpilger26/NumCpp">GitHub Repository</a></dd></dl>
<dl class="section version"><dt>Version</dt><dd>1.3</dd></dl>
<h1><a class="anchor" id="License"></a>
License</h1>
<p>Copyright 2020 David Pilger</p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<h1><a class="anchor" id="Testing"></a>
Testing</h1>
<p>Compiled and tested with Visual Studio 2017/2019, and g++ 7.3.0/8.0, clang 6.0, with Boost version 1.68 and 1.70. Example for using the linalg::gaussNewtonNlls function</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="_num_cpp_8hpp.html">NumCpp.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;cstdlib&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using</span> FunctionType = std::function&lt;double(const nc::NdArray&lt;double&gt;&amp;, <span class="keyword">const</span> <a name="_a0"></a><a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp;)&gt;;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> wikipediaExample()</div><div class="line">{</div><div class="line">    <span class="comment">// https://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm</span></div><div class="line"></div><div class="line">    <span class="comment">// In a biology experiment studying the relation between substrate concentration [S] and reaction rate in</span></div><div class="line">    <span class="comment">// an enzyme-mediated reaction, the data in the following table were obtained.</span></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> sMeasured = { 0.038, 0.194, 0.425, 0.626, 1.253, 2.5, 3.74 };</div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> rateMeasured = { 0.05, 0.127, 0.094, 0.2122, 0.2729, 0.2665, 0.3317 };</div><div class="line"></div><div class="line">    <span class="comment">// It is desired to find a curve (model function) of the form</span></div><div class="line">    FunctionType <span class="keyword">function</span> = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> s = coordinates.<a name="a1"></a><a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> (beta1 * s) / (beta2 + s);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta1</span></div><div class="line">    FunctionType delFdelBeta1 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> s = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> s / (beta2 + s);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta2</span></div><div class="line">    FunctionType delFdelBeta2 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> s = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> -(beta1 * s) / <a name="a2"></a><a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(beta2 + s);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// starting with the initial estimates of beta1Guess and beta2Guess and calculating after 5 iterations</span></div><div class="line">    <span class="keyword">const</span> <a class="code" href="namespacenc.html#aacba4d66f94f6f53637e23c5d82e20d8">nc::uint32</a> numIterations = 5;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta1Guess = 0.9;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta2Guess = 0.2;</div><div class="line"></div><div class="line">    <span class="keyword">auto</span> [betas, <a name="a3"></a><a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a>] = <a name="a4"></a><a class="code" href="namespacenc_1_1linalg.html#aff0f97e94666284100b584e13d27def3">nc::linalg::gaussNewtonNlls</a>(numIterations, sMeasured.<a name="a5"></a><a class="code" href="classnc_1_1_nd_array.html#aae29c9fbae3e92ce432703ffd7722d8b">transpose</a>(), rateMeasured,</div><div class="line">        <span class="keyword">function</span>, {delFdelBeta1, delFdelBeta2}, beta1Guess, beta2Guess);</div><div class="line"></div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;==========Wikipedia Example==========\n&quot;</span>;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;beta values = &quot;</span> &lt;&lt; betas;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;RMS = &quot;</span> &lt;&lt; <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a> &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> exponentialExample()</div><div class="line">{</div><div class="line">    <span class="comment">// United States population (in millions) and the corresponding year:</span></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> year = nc::arange&lt;double&gt;(1.0, 9.0); <span class="comment">// just use time points rather than the year</span></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> population = { 8.3, 11.0, 14.7, 19.7, 26.7, 35.2, 44.4, 55.9 };</div><div class="line"></div><div class="line">    <span class="comment">// It is desired to find a curve (model function) of the form</span></div><div class="line">    FunctionType exponentialFunction = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> beta1 * <a name="a6"></a><a class="code" href="namespacenc.html#ab138b681799463226a8ef60f87d30ac7">nc::exp</a>(beta2 * t);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta1</span></div><div class="line">    FunctionType delFdelBeta1 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> <a class="code" href="namespacenc.html#ab138b681799463226a8ef60f87d30ac7">nc::exp</a>(beta2 * t);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta2</span></div><div class="line">    FunctionType delFdelBeta2 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> beta1 * t * <a class="code" href="namespacenc.html#ab138b681799463226a8ef60f87d30ac7">nc::exp</a>(beta2 * t);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// starting with the initial estimates of beta1Guess and beta2Guess and calculating after 5 iterations</span></div><div class="line">    <span class="keyword">const</span> <a class="code" href="namespacenc.html#aacba4d66f94f6f53637e23c5d82e20d8">nc::uint32</a> numIterations = 5;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta1Guess = 6.0;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta2Guess = 0.3;</div><div class="line"></div><div class="line">    <span class="keyword">auto</span> [betas, <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a>] = <a class="code" href="namespacenc_1_1linalg.html#aff0f97e94666284100b584e13d27def3">nc::linalg::gaussNewtonNlls</a>(numIterations, year.transpose(), population,</div><div class="line">        exponentialFunction, {delFdelBeta1, delFdelBeta2}, beta1Guess, beta2Guess);</div><div class="line"></div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;==========Exponential Population Example==========\n&quot;</span>;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;beta values = &quot;</span> &lt;&lt; betas;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;RMS = &quot;</span> &lt;&lt; <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a> &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> sinusoidalExample()</div><div class="line">{</div><div class="line">    <span class="comment">// Average monthly high temperatures for Baton Rouge, LA:</span></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> month = nc::arange&lt;double&gt;(1.0, 13.0);</div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> temperature = { 61.0, 65.0, 72.0, 78.0, 85.0, 90.0, 92.0, 92.0, 88.0, 81.0, 72.0, 63.0 };</div><div class="line"></div><div class="line">    <span class="comment">// It is desired to find a curve (model function) of the form</span></div><div class="line">    FunctionType sinusodialFunction = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta3 = betas.at(2);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta4 = betas.at(3);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> beta1 * <a name="a7"></a><a class="code" href="namespacenc.html#a0d66dc207eb871bba26bff4838992fef">nc::sin</a>(beta2 * t + beta3) + beta4;</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta1</span></div><div class="line">    FunctionType delFdelBeta1 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta3 = betas.at(2);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> <a class="code" href="namespacenc.html#a0d66dc207eb871bba26bff4838992fef">nc::sin</a>(beta2 * t + beta3);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta2</span></div><div class="line">    FunctionType delFdelBeta2 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta3 = betas.at(2);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> beta1 * t * <a name="a8"></a><a class="code" href="namespacenc.html#affd0f5b31b2bd1b9cea9ab54e25dfab2">nc::cos</a>(beta2 * t + beta3);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta3</span></div><div class="line">    FunctionType delFdelBeta3 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> t = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta1 = betas.at(0);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta2 = betas.at(1);</div><div class="line">        <span class="keyword">const</span> <span class="keywordtype">double</span> beta3 = betas.at(2);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> beta1 * <a class="code" href="namespacenc.html#affd0f5b31b2bd1b9cea9ab54e25dfab2">nc::cos</a>(beta2 * t + beta3);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of function with respect to beta4</span></div><div class="line">    FunctionType delFdelBeta4 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; <span class="comment">/*coordinates*/</span>, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; <span class="comment">/*betas*/</span>) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 1.0;</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// starting with the initial estimates and calculating after 5 iterations</span></div><div class="line">    <span class="keyword">const</span> <a class="code" href="namespacenc.html#aacba4d66f94f6f53637e23c5d82e20d8">nc::uint32</a> numIterations = 5;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta1Guess = 17.0;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta2Guess = 0.5;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta3Guess = 10.5;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> beta4Guess = 77.0;</div><div class="line"></div><div class="line">    <span class="keyword">auto</span> [betas, <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a>] = <a class="code" href="namespacenc_1_1linalg.html#aff0f97e94666284100b584e13d27def3">nc::linalg::gaussNewtonNlls</a>(numIterations, month.transpose(), temperature,</div><div class="line">        sinusodialFunction, {delFdelBeta1, delFdelBeta2, delFdelBeta3, delFdelBeta4},</div><div class="line">        beta1Guess, beta2Guess, beta3Guess, beta4Guess);</div><div class="line"></div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;==========Sinusodial Temperature Example==========\n&quot;</span>;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;beta values = &quot;</span> &lt;&lt; betas;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;RMS = &quot;</span> &lt;&lt; <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a> &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// https://en.wikipedia.org/wiki/Gaussian_function</span></div><div class="line"><span class="keywordtype">double</span> baseGaussianFunction(<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas)</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> x = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> y = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(1);</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> a = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> x0 = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(2);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> y0 = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(3);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> sigmaX = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(4);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> sigmaY = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(5);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> a * <a class="code" href="namespacenc.html#ab138b681799463226a8ef60f87d30ac7">nc::exp</a>(-(<a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(x - x0) / (2.0 * <a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(sigmaX)) + <a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(y - y0) / (2.0 * <a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(sigmaY))));</div><div class="line">};</div><div class="line"></div><div class="line"><span class="comment">// It is desired to find a curve (model function) of the form</span></div><div class="line"><span class="keywordtype">double</span> gaussianFunction(<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas)</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> dcOffset = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(1);</div><div class="line">    <span class="keywordflow">return</span> baseGaussianFunction(coordinates, betas) + dcOffset;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> twoDimensionalGaussianExample()</div><div class="line">{</div><div class="line">    <span class="comment">// create some data points to describe a two-dimensional gaussian function</span></div><div class="line">    <span class="keyword">auto</span> coords = nc::arange&lt;double&gt;(-3.0, 3.1, 0.1);</div><div class="line">    <span class="keyword">auto</span> [y, x] = nc::meshgrid&lt;double&gt;(coords, coords);</div><div class="line">    <span class="keyword">auto</span> coordinates = <a name="a9"></a><a class="code" href="namespacenc.html#a8dec1ff4db1d89ab4b3a7d32ff4b5cf3">nc::vstack</a>({x.flatten(), y.flatten()}).<a name="a10"></a><a class="code" href="namespacenc.html#a4f2a0ece915add7cde89fcfefa30088d">transpose</a>();</div><div class="line"></div><div class="line">    <span class="comment">// randomize some truth values for the model parameters</span></div><div class="line">    <a name="a11"></a><a class="code" href="namespacenc_1_1random.html#ae71a0895ba1f3bff56f78300e3d8538a">nc::random::seed</a>(666); <span class="comment">// set the random seed for repeatability</span></div><div class="line"></div><div class="line">    <span class="comment">// make some random &quot;truth&quot; parameters for generating data</span></div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> aTruth = nc::random::randFloat&lt;double&gt;(900.0, 1100.0);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> dcOffsetTruth = nc::random::randFloat&lt;double&gt;(90.0, 100.0);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> x0Truth = nc::random::randFloat&lt;double&gt;(-0.5, 0.5);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> y0Truth = nc::random::randFloat&lt;double&gt;(-0.5, 0.5);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> sigmaXTruth = nc::random::randFloat&lt;double&gt;(0.9, 1.1);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> sigmaYTruth = nc::random::randFloat&lt;double&gt;(0.9, 1.1);</div><div class="line"></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> betasTruth = { aTruth, dcOffsetTruth, x0Truth, y0Truth, sigmaXTruth, sigmaYTruth };</div><div class="line"></div><div class="line">    <span class="comment">// make some &quot;measurements&quot;</span></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> measurements(1, coordinates.<a name="a12"></a><a class="code" href="classnc_1_1_nd_array.html#a232a5e8c426b629dcda9f9d52e8ce905">shape</a>().<a name="a13"></a><a class="code" href="classnc_1_1_shape.html#a6f89f699dea6eb89eef19e00c92b223a">rows</a>);</div><div class="line">    <span class="keyword">const</span> <span class="keyword">auto</span> cSlice = coordinates.<a name="a14"></a><a class="code" href="classnc_1_1_nd_array.html#a377c1b5393aabc2b00496643a5c5da03">cSlice</a>();</div><div class="line">    <span class="keywordflow">for</span> (<a class="code" href="namespacenc.html#aacba4d66f94f6f53637e23c5d82e20d8">nc::uint32</a> i = 0; i &lt; measurements.size(); ++i)</div><div class="line">    {</div><div class="line">        measurements[i] = gaussianFunction(coordinates(i, cSlice), betasTruth);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// add some noise to the measurements</span></div><div class="line">    <span class="keyword">auto</span> noise = nc::random::randN&lt;double&gt;(measurements.shape()) * 10.0; <span class="comment">// STD = 10 of noise</span></div><div class="line">    measurements += noise;</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of gaussianFunction with respect to a</span></div><div class="line">    FunctionType delFdelA = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> a = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line">        <span class="keywordflow">return</span> baseGaussianFunction(coordinates, betas) / a;</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of gaussianFunction with respect to dcOffset</span></div><div class="line">    FunctionType delFdelDcOffset = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; <span class="comment">/*coordinates*/</span>, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; <span class="comment">/*betas*/</span>) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 1.0;</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of gaussianFunction with respect to x0</span></div><div class="line">    FunctionType delFdelX0 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> x = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line"></div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> x0 = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(2);</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> sigmaX = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(4);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> (x - x0) / <a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(sigmaX) * baseGaussianFunction(coordinates, betas);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of gaussianFunction with respect to y0</span></div><div class="line">    FunctionType delFdelY0 = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> y = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(1);</div><div class="line"></div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> y0 = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(3);</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> sigmaY = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(5);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> (y - y0) / <a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(sigmaY) * baseGaussianFunction(coordinates, betas);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of gaussianFunction with respect to sigmaX</span></div><div class="line">    FunctionType delFdelSigmaX = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> x = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(0);</div><div class="line"></div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> x0 = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(2);</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> sigmaX = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(4);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> (<a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(x - x0) / <a name="a15"></a><a class="code" href="namespacenc.html#a1f059635ecfc805979db6973dfa29008">nc::power</a>(sigmaX, 3)) * baseGaussianFunction(coordinates, betas);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// partial derivative of gaussianFunction with respect to sigmaY</span></div><div class="line">    FunctionType delFdelSigmaY = [](<span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; coordinates, <span class="keyword">const</span> <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a>&amp; betas) -&gt; <span class="keywordtype">double</span></div><div class="line">    {</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> y = coordinates.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(1);</div><div class="line"></div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> y0 = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(2);</div><div class="line">        <span class="keyword">const</span> <span class="keyword">auto</span> sigmaY = betas.<a class="code" href="classnc_1_1_nd_array.html#ae6c1faadee65ae35236c3551784cd5f1">at</a>(4);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> (<a class="code" href="namespacenc.html#a282e72a93afe2e6554e0f17f21dd7b9e">nc::square</a>(y - y0) / <a class="code" href="namespacenc.html#a1f059635ecfc805979db6973dfa29008">nc::power</a>(sigmaY, 3)) * baseGaussianFunction(coordinates, betas);</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="comment">// starting with the initial estimates of and calculating after 5 iterations</span></div><div class="line">    <span class="keyword">const</span> <a class="code" href="namespacenc.html#aacba4d66f94f6f53637e23c5d82e20d8">nc::uint32</a> numIterations = 5;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> aGuess = aTruth + nc::random::randN&lt;double&gt;() * 5.0;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> dcOffsetGuess = dcOffsetTruth + nc::random::randN&lt;double&gt;() * 5.0;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> x0Guess = x0Truth + nc::random::randN&lt;double&gt;() * 0.2;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> y0Guess = y0Truth + nc::random::randN&lt;double&gt;() * 0.2;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> sigmaXGuess = sigmaXTruth + nc::random::randN&lt;double&gt;() * 0.2;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">double</span> sigmaYGuess = sigmaYTruth + nc::random::randN&lt;double&gt;() * 0.2;</div><div class="line"></div><div class="line">    <span class="keyword">auto</span> [betas, <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a>] = <a class="code" href="namespacenc_1_1linalg.html#aff0f97e94666284100b584e13d27def3">nc::linalg::gaussNewtonNlls</a>(numIterations, coordinates, measurements,</div><div class="line">        FunctionType(gaussianFunction), {delFdelA, delFdelDcOffset, delFdelX0, delFdelY0, delFdelSigmaX, delFdelSigmaY},</div><div class="line">        aGuess, dcOffsetGuess, x0Guess, y0Guess, sigmaXGuess, sigmaYGuess);</div><div class="line"></div><div class="line">    <a class="code" href="classnc_1_1_nd_array.html">nc::NdArray&lt;double&gt;</a> initialGuess = { aGuess, dcOffsetGuess, x0Guess, y0Guess, sigmaXGuess, sigmaYGuess };</div><div class="line"></div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;==========2D Gaussian Example==========\n&quot;</span>;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;truth values  = &quot;</span> &lt;&lt; betasTruth;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;initial guess = &quot;</span> &lt;&lt; initialGuess;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;beta values   = &quot;</span> &lt;&lt; betas;</div><div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;RMS = &quot;</span> &lt;&lt; <a class="code" href="namespacenc.html#a2e7c6950a2be94b7162fe0017fd43352">rms</a> &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    wikipediaExample();</div><div class="line">    exponentialExample();</div><div class="line">    sinusoidalExample();</div><div class="line">    twoDimensionalGaussianExample();</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> EXIT_SUCCESS;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
